<template>
  <el-dialog :title="title" :visible.sync="visible" width="1200px" append-to-body>
    <el-form ref="form" inline :model="form" :rules="rules" label-width="170px">
    <!--  基础信息    -->
      <el-form-item label="门市部名称" prop="storeName">
        <el-input
          v-model="form.storeName"
          placeholder="请输入门市部名称"
          clearable
          style="width: 240px"
        />
      </el-form-item>

      <div class="titleDiv">联系人信息</div>
      <el-divider/>
      <el-button size="small" type="primary"
                 icon="el-icon-plus"
                 @click="addContract">
        添加联系人
      </el-button>
      <div class="gx-page-fix-pad"></div>
      <el-table
        :data="form.contractList"
        style="width: 100%">
        <el-table-column prop="headName" label="负责人" width="180">
          <template slot-scope="scope">
            <el-input v-model="scope.row.headName" clearable style="width: 240px"/>
          </template>
        </el-table-column>
        <el-table-column prop="contractStr" label="联系地址" width="180">
          <template slot-scope="scope">
            <el-input v-model="scope.row.contractStr" clearable style="width: 240px"/>
          </template>
        </el-table-column>
        <el-table-column prop="telNumber" label="联系电话">
          <template slot-scope="scope">
            <el-input v-model="scope.row.telNumber" clearable style="width: 240px"/>
          </template>
        </el-table-column>
        <el-table-column prop="weChat" label="微信号">
          <template slot-scope="scope">
            <el-input v-model="scope.row.weChat" clearable style="width: 240px"/>
          </template>
        </el-table-column>
        <el-table-column prop="qqStr" label="QQ">
          <template slot-scope="scope">
            <el-input v-model="scope.row.qqStr" clearable style="width: 240px"/>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.$index)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {add, getById, updateById} from "@/api/system/store";
import {selectByCode} from "@/api/tool/public";

export default {
  name: "create",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 表单参数
      form: {
        "id":'',
        "storeName": "",
        "contractList": [
          {
            headName:'',
            contractStr:'',
            telNumber:'',
            weChat:'',
            qqStr:'',
          }
        ]
      },
      autoConfirmFlag: [],
      // 表单校验
      rules: {
        storeName: [
          {required: true, message: "门市部名称不能为空", trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    deleteTr(index){
      this.tableData.splice(index,1)
    },
    open(id) {
      if (id) {
        getById(id).then(response => {
          this.visible = true
          this.$nextTick(() => {
            this.form = response.data
          })
          this.title = "修改门市"
        })
      } else {
        this.visible = true
        this.$nextTick(() => {
          this.reset()
        })
        this.title = "新增门市"
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != '') {
            updateById(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.visible = false
              this.$parent.getList()
            })
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.visible = false
              this.$parent.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(index) {
      this.form.contractList.splice(index, 1);
    },
    addContract() {
      let contract={
        headName:'',
        contractStr:'',
        telNumber:'',
        weChat:'',
        qqStr:'',
      }
      this.form.contractList.push(contract);
    },
    // 取消按钮
    cancel() {
      this.visible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.$refs.form.resetFields()
    },
  }
}
</script>
<style lang="scss" scoped>
.titleDiv{
  color: #1E5494;
  margin-left: 30px;
}
</style>
